package com.yodawnla.easyPuyo.scene;

import java.util.ArrayList;

import org.anddev.andengine.entity.sprite.Sprite;

import android.view.KeyEvent;

import com.yodawnla.easyPuyo.R;
import com.yodawnla.easyPuyo.HUD.CharacterStateHud;
import com.yodawnla.easyPuyo.HUD.ItemDetailHud;
import com.yodawnla.easyPuyo.HUD.OkWindow;
import com.yodawnla.easyPuyo.HUD.Window.IButtonClickListener;
import com.yodawnla.easyPuyo.HUD.YesNoWindow;
import com.yodawnla.easyPuyo.character.Player;

import com.yodawnla.easyPuyo.item.Equipment;
import com.yodawnla.easyPuyo.item.ItemMgr;
import com.yodawnla.easyPuyo.stage.StageMgr;
import com.yodawnla.easyPuyo.system.Fonts;
import com.yodawnla.easyPuyo.system.SE;
import com.yodawnla.easyPuyo.system.Tex;
import com.yodawnla.lib.YoScene;
import com.yodawnla.lib.util.widget.YoButton;
import com.yodawnla.lib.util.widget.YoText;

public class ShopScene extends YoScene
{
	Player mPlayer = Player.getInstance();

	int mCurrentID = -1;
	int mListAmount = 4;

	YesNoWindow mYesNoWindow = YesNoWindow.getInstance();
	OkWindow mOkWindow = OkWindow.getInstance();

	YoText mGoldText;
	// *******************************************************************************************//
	ArrayList<Equipment> mItemSellList = new ArrayList<Equipment>();

	Sprite mItemSelector;
	int mSelectedItemID;

	// *******************************************************************************************//

	@Override
	public void loadScene()
	{
		inital();
	}

	@Override
	public void onEnterScene()
	{
		ItemDetailHud.getInstance().show(mItemSellList.get(mSelectedItemID));
		ItemDetailHud.getInstance().show();
		ItemDetailHud.getInstance().setPanelPosition(22, 122);
		CharacterStateHud.getInstance().show();
		CharacterStateHud.getInstance().updateCharValue();
	}

	@Override
	public void onExitScene()
	{
		ItemDetailHud.getInstance().hide();

	}

	// *******************************************************************************************//
	public void inital()
	{
		mGoldText = new YoText(300, 5, Fonts.White20, 10);
		this.attachChild(mGoldText);
		sedGoldText();

		// *******************************Initial shop item list**************************************//
		Sprite shopListBg = new Sprite(0, 0, 480, 800, getTexture(Tex.BaseBg));
//		mShopListBg.setColor(39 / 255f, 31 / 255f, 23 / 255f);
		this.attachChild(shopListBg);

		YoButton firstBtn = null;
		for (int i = 0; i < mListAmount; i++)
		{
			final int index = i;

			int minValue = mPlayer.getLevel() + StageMgr.getInstance().getTotalProgress();
			int maxValue = mPlayer.getLevel() + StageMgr.getInstance().getTotalProgress() * 3;
			Equipment item = ItemMgr.getInstance().createRandomEquipment(minValue, maxValue, false);

			mItemSellList.add(item);

			YoButton buyBtn = new YoButton(mScene, 320, 5, getTexture(Tex.Button2))
			{
				@Override
				public void init()
				{
					YoText text = new YoText(5, 5, Fonts.White20, getRString(R.string.Buy));
					this.attachChild(text);
					text.setPosition((this.getWidth() - text.getWidth()) /2, (this.getHeight() - text.getHeight()) /2);
				}

				@Override
				public void onClickedEvent()
				{
					playSound(SE.Click);
					mCurrentID = index;
					purchasable(mCurrentID);
				}
			};

			YoButton itemBtn = new YoButton(this, 5, 270 + i * 90, getTexture(Tex.ItemBar))
			{
				@Override
				public void onClickedEvent()
				{
					ItemDetailHud.getInstance().show(mItemSellList.get(index));
					mSelectedItemID = index;
					mItemSelector.setPosition(this);
				}
			};
			shopListBg.attachChild(itemBtn);
			itemBtn.attachChild(buyBtn);

			Sprite itemIconSprite = new Sprite(10, 0, getTexture(item.getTextureName()));
			itemBtn.attachChild(itemIconSprite);

			YoText itemText = new YoText(120, 7, Fonts.White20, "", 30);
			itemText.setText(getRString(R.string.Price) + ":" + mItemSellList.get(i).getPrice()); // TODO RSTRING
			itemBtn.attachChild(itemText);
			
			if (i == 0)
				firstBtn = itemBtn;
		}

		mItemSelector = new Sprite(0, 0, getTexture(Tex.ShopItemSelector));
		this.attachChild(mItemSelector);
		mItemSelector.setPosition(firstBtn);
	}

	// *******************************************************************************************//
	public void buyItem(int itemID)
	{
		Equipment shopitem = mItemSellList.get(itemID);

		mPlayer.addItem(ItemMgr.getInstance().createEquipment(true, shopitem.getBinaryType(), shopitem.getNotZeroValueListArray()));

		mPlayer.modifyGold(-shopitem.getPrice());

		CharacterStateHud.getInstance().updateCharValue();

	}

	public void sedGoldText()
	{
		if (mGoldText != null)
			mGoldText.setText(getRString(R.string.Gold) + ":" + mPlayer.getGold());
	}

	// *******************************************************************************************//
	/* expend player hold gold to determine item if purchasable. */
	public void purchasable(int itemID)
	{

		int tmpGold = mPlayer.getGold();
		final int tmpPrice = mItemSellList.get(itemID).getPrice();

		if (tmpGold >= tmpPrice)
			mYesNoWindow.show(getRString(R.string.hud_BuyItem) + "?", new IButtonClickListener() // TODO RSTRING
					{

						public void onOkClicked()
						{
							playSound(SE.Click);
							buyItem(mCurrentID);
						}

						public void onCancelClicked()
						{
							playSound(SE.Click);
							mYesNoWindow.hide();
						}
					});
		else
			mOkWindow.show(getRString(R.string.Gold) + " " + getRString(R.string.hud_IsNotEnough)); // TODO RSTRING
	}

	// *******************************************************************************************//

	@Override
	public boolean onKeyDown(final int pKeyCode, final KeyEvent pEvent)
	{
		switch (pKeyCode)
		{
		case KeyEvent.KEYCODE_BACK:
			if (pEvent.getAction() == KeyEvent.ACTION_DOWN)
			{

				return false;
			}
			break;
		case KeyEvent.KEYCODE_MENU:
			if (pEvent.getAction() == KeyEvent.ACTION_DOWN)
			{

				return true;
			}
			break;
		}
		return false;
	}
}
